
const {
    app, BrowserWindow, screen, globalShortcut, shell,
    dialog, Tray, Menu, Notification
    
} = require('electron');

app.allowRendererProcessReuse = true;

const path = require('path');
const core = require('./mainlib/core');
const fs = require('fs');

const projectPath = app.getAppPath();
const srcPath = projectPath + '/src';

let mainWindow;
let tray = null;
let setting = core.getSetting();

global.projectPath = projectPath;
global.srcPath = srcPath;

/**
 * 创建主窗口
 * @returns {Electron.BrowserWindow}
 */
function createWindow() {
    // Create the browser window.
    let mainWindow = new BrowserWindow({
        width: 800,
        height: 600,
        frame: false,
        transparent: true,
        show:setting.base.nohide,
        // show:true,
        resizable: false,
        title: '快速查找',
        alwaysOnTop:true,
        webPreferences: {
            nodeIntegration: true,
            // preload: path.join(__dirname, 'preload.js')
        }
    });

    if(core.isInAsar()) {
        mainWindow.removeMenu();
    }

    mainWindow.animateShow = function() {
        mainWindow.setAlwaysOnTop(true);
        core.showWindow(this);
    };
    
    mainWindow.animateHide = function() {
        mainWindow.setAlwaysOnTop(false);
        core.hideWindow(this);
    };
    
    
    mainWindow.on('blur', function () {
        if(!setting.base.nohide) {
            core.hideWindow(mainWindow);
        }
    });
    
    let windowBounds = core.getMainWindowBounds();
    mainWindow.setBounds(windowBounds);
    
    // and load the index.html of the app.
    mainWindow.loadFile(path.join(__dirname, 'window\\main-window.html'))
    
    mainWindow.on('close', e => {
        core.hideWindow(mainWindow);
        e.preventDefault();
        e.returnValue=false;
    });
    
    return mainWindow;
}

function showMainWindow() {
    mainWindow.animateShow();
    mainWindow.focus();
}

function hideMainWindow() {
    mainWindow.animateHide();
}

/**
 * 注册快捷键
 */
function registerKey() {
    const key = "Super+`";
    
    let ret = globalShortcut.register(key, () => {
        if (mainWindow.isVisible()) {
            hideMainWindow();
        } else {
            showMainWindow();
        }
    });

    if(!ret) {
        dialog.showErrorBox('启动错误','热键 win+` 注册失败');
        app.exit();
    }
}


/**
 * app.on ready
 */
function start() {
    let packageData = JSON.parse(fs.readFileSync(app.getAppPath() + '\\package.json').toString());
    
    core.on(core.transferMessageKey, function (args) {
        core.trigger(args[0], args[1]);
    });
    
    mainWindow = createWindow();
    global.mainWindow = mainWindow;
    registerKey();
    
    core.on(core.ET.SETTING_CHANGE, () => {
        setting = core.getSetting();
    
        if(!setting.base.nohide) {
            core.hideWindow(mainWindow);
        }
    });

    tray = new Tray(`${projectPath}/icon.png`)
    
    let menu = Menu.buildFromTemplate([{ 
        label: '退出', click() {
            app.exit(); 
        } 
    }])

    // Call this again for Linux because we modified the context menu
    tray.setContextMenu(menu);

    tray.addListener('click', function () {
        showMainWindow();
    });
    
    // let startupNotification = new Notification({
    //     title:'Quick Start ' + packageData.version + ' 正在后台运行'
    // });

    // startupNotification.show();
    // WndMgr.listen();
}

app.whenReady().then(start);

